//
//  FileManagerExtension.swift
//  CommonFoundation
//
//  Created by raniys on 1/5/18.
//  Copyright © 2018 raniys. All rights reserved.
//

import Foundation

public extension FileManager {
    
    /// clear the tmp directory
    ///
    /// - Parameter completion: result block
    func clearTmpDirectory(completion: @escaping (Bool, String) -> ()) {
        do {
            let tmpDirectory = try contentsOfDirectory(atPath: NSTemporaryDirectory())
            try tmpDirectory.forEach {[unowned self] file in
                let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
                try self.removeItem(atPath: path)
            }
            completion(true, "Clear tmp data succeed")
        } catch {
            print(error)
            completion(false, "Clear tmp data failed, error：" + error.localizedDescription)
        }
    }
    
    /// calculate the size of tmp directory
    ///
    /// - Returns: size of tmp directory
    func sizeTmpDirectory() -> Int64 {
        do {
            var tempSize: Int64 = 0
            let tmpDirectory = try contentsOfDirectory(atPath: NSTemporaryDirectory())
            try tmpDirectory.forEach {[unowned self] file in
                let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
                let size = try self.attributesOfItem(atPath: path)[FileAttributeKey.size] as! NSNumber
                tempSize += size.int64Value
            }
            return tempSize
        } catch {
            print(error)
            return 0
        }
    }
    
    
    func sizeCacheDirectory() -> Int64 {
        
        let fileUrls = urls(for: .cachesDirectory, in: .userDomainMask)
        
        var tempSize: Int64 = 0
        fileUrls.forEach { (url) in
            do {
                let fileNames = try contentsOfDirectory(atPath: url.path)
                for fileName in fileNames {
                    let path = "\(url.path)/\(fileName)"
                    let attrSize = try self.attributesOfItem(atPath: path)[FileAttributeKey.size]
                    
                    guard let size = attrSize as? NSNumber else {
                        continue
                    }
                    tempSize += size.int64Value
                }
            } catch {
                print(error)
            }
        }
        
        return tempSize
    }
}


public extension FileManager {

    class func pathFor(directory: SearchPathDirectory) -> String {
        return NSSearchPathForDirectoriesInDomains(directory, .userDomainMask, true)[0]
    }
    
    class func urlFor(directory: SearchPathDirectory) -> URL {
        return FileManager.default.urls(for: directory, in: .userDomainMask).last!
    }
    
    class func documentsURL() -> URL {
        return urlFor(directory: FileManager.SearchPathDirectory.documentDirectory)
    }
    
    class func documentsPath() -> String {
        return pathFor(directory: FileManager.SearchPathDirectory.documentDirectory)
    }
    
    class func libraryURL() -> URL {
        return urlFor(directory: FileManager.SearchPathDirectory.libraryDirectory)
    }
    
    class func libraryPath() -> String {
        return pathFor(directory: FileManager.SearchPathDirectory.libraryDirectory)
    }
    
    class func cachesURL() -> URL {
        return urlFor(directory: FileManager.SearchPathDirectory.cachesDirectory)
    }
    
    class func cachesPath() -> String {
        return pathFor(directory: FileManager.SearchPathDirectory.cachesDirectory)
    }
}

